package cn.sxt.shop.users.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.sxt.shop.common.util.PageInfoUtil;
import cn.sxt.shop.users.dao.IAAdminsDao;
import cn.sxt.shop.users.pojo.AAdmins;
import cn.sxt.shop.users.service.IUsersService;

@Service("usersService")
public class UsersServiceImpl implements IUsersService{

	@Resource
	private IAAdminsDao adminsDao ;
	
	/* 管理员模块开始 */
	/**
	 * 查询一条管理员
	 */
	@Override
	public AAdmins findOneAdminsService(Map<String, Object> condMap) {
		return this.adminsDao.findOne(condMap);
	}

	/**
	 * 查询多条管理员
	 */
	@Override
	public List<AAdmins> findListAdminsService(PageInfoUtil pageInfoUtil,Map<String, Object> condMap) {
		if(pageInfoUtil != null)
		{
			/* 查询分页 */
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			List<AAdmins> adminsList = this.adminsDao.findList(condMap);
			/* 设置总记录数 */
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() + ""));
			return adminsList ; 
		}
		/* 查询不分页 */
		return this.adminsDao.findList(condMap);
	}

	/**
	 * 保存一条管理员
	 */
	@Override
	public JSONObject saveOneAdminsService(AAdmins admins) {
		JSONObject resultJSON = new JSONObject(); 
		int res = this.adminsDao.saveOne(admins);
		if(res > 0 )
		{
			/* 存储数据 */
			JSONObject dataJSON = new JSONObject() ; 
			dataJSON.put("id", admins.getId());
			dataJSON.put("effect", res);
			
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "1");
			resultJSON.put("info", "成功");
		}else
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "失败");
		}
		return resultJSON;
	}

	/**
	 * 更新一条管理员
	 */
	@Override
	public JSONObject updateOneAdminsService(AAdmins admins) {
		JSONObject resultJSON = new JSONObject(); 
		int res = this.adminsDao.updateOne(admins);
		if(res > 0 )
		{
			/* 存储数据 */
			JSONObject dataJSON = new JSONObject() ; 
			dataJSON.put("id", admins.getId());
			dataJSON.put("effect", res);
			
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
		}
		return resultJSON;
	}

	/**
	 * 删除一条管理员
	 */
	@Override
	public JSONObject deleteOneAdminsService(AAdmins admins) {
		JSONObject resultJSON = new JSONObject(); 
		int res = this.adminsDao.deleteOne(admins);
		if(res > 0 )
		{
			/* 存储数据 */
			JSONObject dataJSON = new JSONObject() ; 
			dataJSON.put("id", admins.getId());
			dataJSON.put("effect", res);
			
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
		}
		return resultJSON;
	}
	/* 管理员模块结束 */
}
